package com.huidu.bitrade.util.spark;

import cn.hutool.crypto.digest.MD5;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encrypt
{
    private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    public static String MD5(String input)
    {
        return MD5(input, Charset.defaultCharset());
    }

    public static String MD5(String input, String charset)
    {
        return MD5(input, Charset.forName(charset));
    }

    public static String MD5(String input, Charset charset)
    {
        MessageDigest digest = null;
        try
        {
            digest = MessageDigest.getInstance("MD5");
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        digest.update(input.getBytes(charset));
        byte[] out = digest.digest();
        return byteToString(out);
    }

    private static String byteToArrayString(byte bByte)
    {
        int iRet = bByte;
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return hexDigits[iD1] + hexDigits[iD2];
    }

    private static String byteToString(byte[] bByte)
    {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }
}

